package com.zhudao.crawler.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhudao.crawler.util.HttpUtil;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

@Service
public class Crawler58Service {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String str = HttpUtil.get("http://image.songwie.com/city.json");
        JSONObject jsonObject = JSON.parseObject(new String(str.getBytes("iso-8859-1"), "UTF-8"));
        JSONArray area = jsonObject.getJSONObject("area").getJSONObject("result").getJSONArray("area");
        for (int i = 0; i < area.size(); i++) {
            JSONObject jsonObject1 = area.getJSONObject(i);
            Integer pid = jsonObject1.getInteger("pid");
            int id = jsonObject1.getInteger("id");
            if (pid == 2) {
                String name = jsonObject1.getString("name");
                String pinyin = jsonObject1.getString("dirname");
                for (int page = 1; page < 200; page++) {
                    String listInfo = HttpUtil.get("https://app.58.com/dict/list/appxiaoqu?tabkey=allcity&action=getListInfo&curVer=7.15.3&ct=filter&appId=1&localname=sh&os=android&format=json&v=1&geotype=baidu&location=2%2C1411%2C6226&filterParams=%7B%22filterLocal%22%3A%22" + pinyin + "%22%7D&geoia=31.217233%2C121.557881&page=" + page + "&params=%7B%7D");
                    JSONArray getListInfos = JSON.parseObject(listInfo).getJSONObject("result").getJSONObject("getListInfo").getJSONArray("infolist");
                    if (getListInfos.size() == 0) {
                        break;
                    }
                    for (int infoId = 0; infoId < getListInfos.size(); infoId++) {
                        JSONObject xiaoqu = getListInfos.getJSONObject(infoId);
                        Integer xiaoquID = xiaoqu.getInteger("infoID");
                        String xiaoquInfo = HttpUtil.get("https://app.58.com/dict/detail/appxiaoqu/" + xiaoquID + "?localname=sh&format=json");
                        System.out.println(JSON.toJSON(xiaoqu));
                        // System.out.println(JSON.toJSON(xiaoquInfo));
                    }
                }
                for (int y = 0; y < area.size(); y++) {
                    JSONObject jsonObject2 = area.getJSONObject(y);
                    if (id == jsonObject2.getInteger("pid")) {
                        //System.out.println(jsonObject2.getString("name"));
                        //System.out.println(jsonObject2.getString("pinyin"));
                    }
                }
            }
        }
    }
}